package visiopuce.DAO;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Query;

import visiopuce.HibernateUtil;
import visiopuce.objets.Annotation;

public class AnnotationDAOImpl extends GenericDAOImpl<Annotation, BigDecimal>
		implements AnnotationDAO {

	
	/** Constructeur priv� */
	private AnnotationDAOImpl() {
	}

	/** Instance unique non pr�initialis�e */
	private static AnnotationDAOImpl INSTANCE = null;

	/** Point d'acc�s pour l'instance unique du singleton */
	public static AnnotationDAO getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new AnnotationDAOImpl();
		}
		return INSTANCE;
	}

	@Override
	public List<Annotation> getAnnotationByAlteration(Integer idAlteration) {
		List<Annotation> annotations = null;
		String sql = "SELECT a FROM Annotation a WHERE a.alteration.idAlteration = :idAlteration";
		Query query = HibernateUtil.getSession().createQuery(sql)
				.setParameter("idAlteration", idAlteration);
		annotations = findMany(query);
		return annotations;
	}

	@Override
	public int getIdTypeCausalByAlteration(Integer idAlteration) {
		String sql = "SELECT a.causale.idTypeCausal FROM Annotation a WHERE a.alteration.idAlteration = :idAlteration order by a.idAnnotation DESC ";
		Query query = HibernateUtil.getSession().createQuery(sql)
				.setParameter("idAlteration", idAlteration);
		query.setFirstResult(0);
		query.setMaxResults(1);
		int result = 0;
		Integer resul = (Integer) query.uniqueResult();
		if (resul != null){
			result = resul.intValue();
		}
		return result;
	}
	
	

}